from django.db import models


# Create your models here.


class GradeModel(models.Model):  # 等级表
    grade = models.CharField(max_length=20, verbose_name='用户等级')

    class Meta:
        db_table = 'grade'

    def __str__(self):
        return self.grade


class UserModel(models.Model):  # 用户表
    name = models.CharField(max_length=30, verbose_name='用户名')
    password = models.CharField(max_length=200, verbose_name='密码')
    state = models.BooleanField(default=0, verbose_name='状态')
    email = models.EmailField(max_length=60, verbose_name='邮箱')
    grade = models.ForeignKey(GradeModel, on_delete=models.CASCADE, verbose_name='用户等级')

    class Meta:
        db_table = 'user'

    def __str__(self):
        return self.name


class UserInfoModel(models.Model):  # 用户信息
    user = models.OneToOneField(UserModel, on_delete=models.CASCADE, verbose_name='用户，一对一')
    name = models.CharField(max_length=30, verbose_name='真实姓名')
    phone = models.CharField(max_length=11, verbose_name='手机号')
    identity = models.CharField(max_length=18, verbose_name='身份证号')
    qq = models.CharField(max_length=15, verbose_name='qq号')
    site = models.CharField(max_length=200, verbose_name='地址')

    class Meta:
        db_table = 'userinfo'

    def __str__(self):
        return self.name


class AuthorModel(models.Model):  # 作者表
    user = models.OneToOneField(UserModel, on_delete=models.CASCADE, verbose_name='用户，一对一')
    name = models.CharField(max_length=30, verbose_name='作者名')
    state = models.BooleanField(default=0, verbose_name='状态')

    class Meta:
        db_table = 'author'

    def __str__(self):
        return self.name


class CateModel(models.Model):  # 分类表
    name = models.CharField(max_length=30, verbose_name='分类名')

    class Meta:
        db_table = 'cate'

    def __str__(self):
        return self.name


class FictionModel(models.Model):  # 小说表
    name = models.CharField(max_length=30, verbose_name='小说名')
    author = models.ForeignKey(AuthorModel, on_delete=models.CASCADE, verbose_name='作者')
    price = models.IntegerField(verbose_name='价格')
    data = models.DateTimeField(auto_now_add=True, verbose_name='发布日期')  # 日期字段
    img = models.ImageField(upload_to='static/img', verbose_name='图片')
    intro = models.TextField(verbose_name='简介')
    quantity = models.IntegerField(default=0, verbose_name='阅读量')
    cate = models.ForeignKey(CateModel, on_delete=models.CASCADE, verbose_name='分类')

    class Meta:
        db_table = 'fiction'

    def __str__(self):
        return self.name


class SectionModel(models.Model):  # 章节表
    fiction = models.ForeignKey(FictionModel, on_delete=models.CASCADE, verbose_name='小说')
    content = models.TextField(verbose_name='内容')
    name = models.CharField(max_length=100, verbose_name='章节名')

    class Meta:
        db_table = 'section'

    def __str__(self):
        return self.name
